import random
import pandas as pd
from sqlalchemy import create_engine

# 暂定
from bqtool.utils.tools import get_pg


def get_db(user, password, ip, instance='orcl', port='1521'):
    engine_str = 'oracle://%s:%s@%s:%s/%s' % (user, password, ip, port, instance)
    engine = create_engine(engine_str, encoding='utf-8')
    return engine

def read_data(client):
    c = pd.read_sql_query("""
    select t.id,t.deviceid,t.gpstime,t.longtitude,t.latitude,t.devicetype from T_GPS_RECORDINFO_201901 t where t.gpstime between 
    to_date('2019/1/23 10:00:00','yyyy/MM/dd HH24:mi:ss') and to_date('2019/1/23 11:00:00','yyyy/MM/dd HH24:mi:ss')
    """, client)
    print('ok')
    return c


def insert(data, client):
    add = 10 + random.randint(1, 10)
    data = data.iloc[0:add]
    data.to_sql('t_gps_recordinfo_test', client, if_exists='append', index=False)
    print('保存成功')



if __name__ == '__main__':
    # client = get_db('ehl_public', 'ehl1234', '10.2.111.66')
    # c = read_data(client)
    # insert(c, client)
    client = get_pg()
    c = pd.read_sql_query("""
        select * from t_a
        """, client)
    print('ok')
